<template>
    <div>
        <!--会员优惠券-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/customer' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>角色管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="coupFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="coupFormData.couname" placeholder="请输入优惠券名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary"  @click="initCoupTable">查询</el-button>
                    <el-button type="primary" @click="tianjia">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="couname"
                            label="优惠券名称"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="type"
                            label="优惠卷类型"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.type == 0">全场赠券</span>
                            <span v-if="scope.row.type == 1">会员赠券</span>
                            <span v-if="scope.row.type == 2">消费赠券</span>
                            <span v-if="scope.row.type == 3">注册赠券</span>
                        </template>
                    </el-table-column>

                    <el-table-column
                            prop="ycode"
                            label="优惠码"
                            align="center">
                    </el-table-column>

                    <el-table-column
                            prop="platform"
                            label="使用平台"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.platform == 0">全部</span>
                            <span v-if="scope.row.platform == 1">移动</span>
                            <span v-if="scope.row.platform == 2">PC</span>
                        </template>
                    </el-table-column>

                    <el-table-column
                            prop="count"
                            label="数量(/张)"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="amount"
                            label="金额(元)"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="perLimit"
                            label="每人限领(/张)"
                            align="center">
                    </el-table-column>

                    <el-table-column
                            prop="minPoint"
                            label="使用门槛"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.minPoint == 0">全场通用</span>
                            <span v-if="scope.row.minPoint == 1">指定分类</span>
                            <span v-if="scope.row.minPoint == 2">指定车辆</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="startTime"
                            label="开始时间"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="endTime"
                            label="结束时间"
                            align="center">
                    </el-table-column>

                    <el-table-column
                            prop="useType"
                            label="使用类型"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.useType == 0">全场通用</span>
                            <span v-if="scope.row.useType == 1">指定分类</span>
                            <span v-if="scope.row.useType == 2">指定车辆</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="publishCount"
                            label="发行数量(/张)"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="useCount"
                            label="已使用数量(/张)"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="receiveCount"
                            label="领取数量(/张)"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="enableTime"
                            label="可领取日期"
                            align="center">
                    </el-table-column>

                    <el-table-column
                            prop="level.levname"
                            label="领取等级"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="note"
                            label="备注"
                            align="center">
                    </el-table-column>

                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="updateCoup(scope.row.id)"></el-button>
                            <br>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteCoup (scope.row.id)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>
        <!--修改的对话框-->

        <el-dialog
                title="提示"
                :visible.sync="editDialogVisible"
                width="30%"
                @close="qx"
        >
            <!--model:表单数据-->
            <el-form  label-width="80px" :model="coupForm" :label-position="labelPosition" ref="coupForm" :rules="ruleForm">
                <el-form-item label="优惠券名称" prop="couname">
                    <el-input v-model="coupForm.couname" placeholder="请输入优惠券名称"></el-input>
                </el-form-item>

                <el-form-item label="优惠卷类型" prop="type">
                    <el-select v-model="coupForm.type" placeholder="请输入优惠卷类型">
                        <el-option label="全场赠券" value="0"></el-option>
                        <el-option label="会员赠券" value="1"></el-option>
                        <el-option label="消费赠券" value="2"></el-option>
                        <el-option label="注册赠券" value="3"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="优惠码" prop="ycode">
                    <el-input v-model="coupForm.ycode" placeholder="请输入优惠码"></el-input>
                </el-form-item>

                <el-form-item label="使用平台" prop="platform">
                    <el-select v-model="coupForm.platform" placeholder="请输入使用平台">
                        <el-option label="全部" value="0"></el-option>
                        <el-option label="移动" value="1"></el-option>
                        <el-option label="PC" value="2"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="数量(/张)" prop="count">
                    <el-input v-model="coupForm.count" placeholder="请输入数量(/张)"></el-input>
                </el-form-item>
                <el-form-item label="金额(/元)" prop="amount">
                    <el-input v-model="coupForm.amount" placeholder="请输入金额(/元)"></el-input>
                </el-form-item>
                <el-form-item label="每人限领(/张)" prop="perLimit">
                    <el-input v-model="coupForm.perLimit" placeholder="请输入每人限领(/张)"></el-input>
                </el-form-item>

                <el-form-item label="使用门槛" prop="minPoint">
                    <el-select v-model="coupForm.minPoint" placeholder="请输入使用门槛">
                        <el-option label="全场通用" value="0"></el-option>
                        <el-option label="指定分类" value="1"></el-option>
                        <el-option label="指定车辆" value="2"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker
                            v-model="coupForm.startTime"
                            type="datetime"
                            value-format="yyyy-MM-ddTHH:mm:ss"
                            placeholder="选择日期时间">
<!--                        value-format="yyyy-MM-ddTHH:mm:ss"-->
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker
                            v-model="coupForm.endTime"
                            type="datetime"
                            value-format="yyyy-MM-ddTHH:mm:ss"
                            placeholder="选择日期时间">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="使用类型" prop="useType">
                    <el-select v-model="coupForm.useType" placeholder="请输入使用类型">
                        <el-option label="全场通用" value="0"></el-option>
                        <el-option label="指定分类" value="1"></el-option>
                        <el-option label="指定车辆" value="2"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="发行数量(/张)" prop="publishCount">
                    <el-input v-model="coupForm.publishCount" placeholder="请输入发行数量(/张)"></el-input>
                </el-form-item>

                <el-form-item label="已使用数量(/张)" prop="useCount">
                    <el-input v-model="coupForm.useCount" placeholder="请输入已使用数量(/张)"></el-input>
                </el-form-item>

                <el-form-item label="领取数量(/张)" prop="receiveCount">
                    <el-input v-model="coupForm.receiveCount" placeholder="请输入领取数量(/张)"></el-input>
                </el-form-item>

                <el-form-item label="可领取日期" prop="enableTime">
                    <el-date-picker
                            v-model="coupForm.enableTime"
                            type="datetime"
                            value-format="yyyy-MM-ddTHH:mm:ss"
                            placeholder="选择可领取日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="领取等级" prop="memberLevel">
<!--                    <el-input v-model="coupForm.memberLevel" ></el-input>-->
                    <el-select v-model="coupForm.memberLevel" placeholder="请输入领取等级">
<!--                        <el-option label="全级可用" value="0"></el-option>-->
                        <el-option v-for="item in levelOptions" :key="item.id" :label="item.levname" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注" prop="note">
                    <el-input v-model="coupForm.note" placeholder="请t添加备注"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="qx">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Level",
        data(){
            let varTime = (rule, value, callback) => {
                 if(this.coupForm.endTime){
                     if (this.coupForm.startTime>this.coupForm.endTime){
                         return callback(new Error("优惠开始时间不能大于结束时间"))
                     }else {
                         return callback();
                     }
                     if (this.coupForm.enableTime>this.coupForm.endTime){
                         return callback(new Error("可领取时间不能大于结束时间"))
                     }else {
                         return callback();
                     }
                 }else {
                     return callback();
                 }
             }
            let varCode = (rule, value, callback) => {
                //查询用户名是否重复
                this.$http.post(`/client/coup/selCode`,this.coupForm).then(function (res) {
                    if (res && res.data.result.code == '0') {

                        callback()
                    } else if (res && res.data.result.code == '1') {
                        callback(new Error('优惠码已存在'))
                    }

                })
            }
            return{
                //修改框内字段名左对齐
                labelPosition: 'left',
                //条件搜索对象
                coupFormData:{},
                //表格数据
                tableData:[],
                //添加修改弹出框显示
                editDialogVisible:false,
                //添加用户表单信息
                coupForm:{
                },
                //当前的显示的页码
                currentPage:1,
                //每页显示的条数
                pageSize:5,
                //总条数
                total:0,
                //下拉
                levelOptions:{},
                //表单校验数据:
                ruleForm:{
                    couname :[
                        { required: true, message: '请输入优惠券名称', trigger: 'blur' },
                    ],memberLevel :[
                        { required: true, message: '请输入会员等级', trigger: 'blur' },
                    ],
                    ycode:[
                        { required: true, message: '请输入优惠码', trigger: 'blur' },
                        { min: 8, max: 16, message: '长度在 8 到 16 个字符', trigger: 'blur' },
                        { validator: varCode, trigger: 'blur' }
                    ],
                    // 开始时间小于结束时间，且开始时间小于此刻
                    startTime: [
                        { required: true, message: '优惠开始时间不能为空'},
                        { validator: varTime}
                    ],
                    // 结束时间大于开始时间，且小于此刻
                    endTime: [
                        { required: true, message: '优惠结束时间不能为空'},
                        { validator: varTime}
                    ],
                    enableTime: [
                        { required: true, message: '可领取时间不能为空'},
                        { validator: varTime}
                    ]
                },
            }
        },
        created() {
            //获取客户的信息
            this.initCoupTable();
            this.xiala();
        },
        methods:{
            //下拉等级
            xiala(){
                var that=this;
                this.$http.post(`/client/lev/findMumber`).then(function(resp){
                    that.levelOptions=resp.data.result.level;
                });
            },
            //分页查询
            initCoupTable(){
                //get方式不支持json数据
                var that=this;
                this.$http.post(`/client/coup/initCoupTable/${this.currentPage}/${this.pageSize}`,this.coupFormData).then(function(resp){
                    console.log(resp);
                    that.tableData=resp.data.result.records;
                    that.total=resp.data.result.total;
                });
            },
            //添加
            tianjia(){
                this.editDialogVisible=true;
                //this.coupForm={};
            },
            qx(){
                this.$refs.coupForm.resetFields();
                this.editDialogVisible=false;
            },
            //删除
            deleteCoup (id){
                var that=this;

                this.$http.delete(`/client/coup/deleteCoup/${id}`).then(function (resp) {
                    that.$message.success(resp.data.message);
                    that.initCoupTable();
                    // location.reload();
                })
                this.currentPage=1;
            },
            //修改的回显
            updateCoup(id){
                this.editDialogVisible=true;
                var that=this;
                this.$http.get(`/client/coup/findCoupById/${id}`).then(function(resp){
                    console.log(resp);
                    that.coupForm=resp.data.result.coupon;
                })
            },
            //提交
            submit(){
                var that=this;
                this.$refs.coupForm.validate((valid)=>{
                    if(valid) {//valid表示校验通过
                        //进行id判断,如果id不为空则回显数据,走修改,如果id为空,则走添加
                        if(this.coupForm.id!=null){
                                          //JSON对象
                                          //修改提交
                            this.$http.put(`/client/coup/updateCoup`,this.coupForm).then(function(resp){
                                console.log(resp)
                                if(resp.data.success==true){
                                    that.editDialogVisible=false;
                                    that.$message.success(resp.data.message);
                                    that.initCoupTable();
                                }
                            })
                        }else{
                            //添加提交
                            this.$http.post(`/client/coup/insertCoup`,this.coupForm).then(function (resp) {
                                if(resp.data.success==true){
                                    that.editDialogVisible=false;
                                    that.$message.success(resp.data.message);
                                    that.initCoupTable();
                                }
                            })
                        }

                    }

                })
            },
            //当改变没有显示的条数时触发该方法
            handleSizeChange(v){
                this.pageSize=v;
                this.initCoupTable();
            },
            //切换页码时触发该方法
            handleCurrentChange(v){
                this.currentPage=v;
                this.initCoupTable();
            },

        },
    }
</script>

<style scoped>

</style>
